
import { Effect, Reducer } from 'umi';
import { getknowledgeList ,getKnowledgeDetail} from '@/services';
import { knowledgeItem , IknowledgeDetail} from '@/types';
// 接口

export interface KnowledgeModelState { 
  knowledgeList: knowledgeItem[]; 
  knowledgeDetail:IknowledgeDetail[]
}

export interface KnowledgeModelType {
  namespace: 'knowledge';
  state: KnowledgeModelState;
  effects: { 
    getknowledgeList: Effect;
    getKnowledgeDetail:Effect;
  };
  reducers: {
    save: Reducer<KnowledgeModelState>;
  }; 
}

const KnowledgeModel: KnowledgeModelType = {
  // 命名空间  等价与 vuex的namespce,相当于redux的子reduce
  namespace: 'knowledge',
  // 定义的转态
  state: { 
    knowledgeList: [],
    knowledgeDetail:[],
  },
  // 副作用,对应的异步操作,常指网络请求

  effects: {
    *getknowledgeList({ payload }, { call, put }) {
      const result = yield call(getknowledgeList);
      console.log(result,'xiaoce');
      
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            knowledgeList: result.data[0],
          }, 
        })
      }
    },
    *getKnowledgeDetail({ payload }, { call, put }) { 
      let result = yield call(getKnowledgeDetail, payload);
      // localStorage.setItem("kid",JSON.stringify(result.data))
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            knowledgeDetail: result.data,
          },
        });
      }
    },
    
  },
  // 同步修改state的操作 
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },

  },

};

export default KnowledgeModel;